package com.blue.tooth.lock.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.blue.tooth.lock.R;
import com.blue.tooth.lock.vo.BlueToothDeviceVo;

import java.util.List;

/**
 * author: jiangliqiang
 * date:   2020/09/21
 */
public class BlueToothDeviceAdapter extends BaseAdapter {

    public BlueToothDeviceAdapter(List<BlueToothDeviceVo> data, Context mContext) {
        this.mContext = mContext;
        this.data = data;
        mInflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        if (data != null) {
            return data.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (data != null) {
            return data.get(position);
        } else {
            return null;
        }
    }

    @Override
    public long getItemId(int position) {
        return (long) position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_device, null);
            viewHolder = new ViewHolder();
            viewHolder.name = convertView.findViewById(R.id.name);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        BlueToothDeviceVo blueToothDeviceVo = data.get(position);
        viewHolder.name.setText(blueToothDeviceVo.getName());
        return convertView;
    }

    private class ViewHolder {
        private TextView name;
    }

    public void clearData() {
        if (data != null) {
            data.clear();
        }
    }

    public void setData(List<BlueToothDeviceVo> data){
        this.data=data;
    };

    private LayoutInflater mInflater;
    private Context mContext;
    private List<BlueToothDeviceVo> data;
}
